<?php
# vip认证订单支付

require __DIR__ . '/../../../public/wechatConfig.php';//引入微信配置为文件//导入微信配置
$database =require __DIR__ . '/../../../application/database.php';

require_once __DIR__."/../notify/lib/WxPay.Api.php";
require_once __DIR__."/../notify/lib/WxPay.JsApiPay.php";
require_once __DIR__."/../notify/lib/WxPay.Config.php";

function response ($code,$msg,$data)
{
    $result ["errorCode"] = $code;
    $result ["errorInfo"] = $msg;//提示信息
    $result ["data"] = $data;
    echo json_encode($result);
    die();
}

$logPath = __DIR__ . '/../../../extend/pay/request/logs/test.log';

function write_file($filename, $text, $openmod = 'a')
{
    if (@$fp = fopen($filename, $openmod)) {
        flock($fp, 2);
        fwrite($fp, $text);
        fclose($fp);
        return true;
    } else {
        return false;
    }
}

$dbms='mysql';     //数据库类型
$host=$database['hostname']; //数据库主机名
$dbName=$database['database'];    //使用的数据库
$user=$database['username'];      //数据库连接用户名
$pass=$database['password'];          //对应的密码
$dsn="$dbms:host=$host;dbname=$dbName";
$dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象

// 订单ID
$oid = (int)$_GET['oid'];

$sql = <<< SQL
SELECT a.id,a.code,a.type,a.user_id,b.amount,b.state,c.nick_name,c.open_id,c.account  
FROM `hfztcy_order` AS a 
    INNER JOIN `hfztcy_order_vip_auth` AS b 
        ON `a`.`id` = `b`.`order_id` 
    INNER JOIN `base_user` AS c 
        ON `b`.`user_id` = `c`.`id` 
WHERE `a`.`id` = {$oid}
SQL;
$orderInfo = $dbh->query($sql)->fetch(2);

if (empty($orderInfo)) {
    write_file($logPath, "订单不存在\r\n");
    response(1,'订单不存在',false);
}

// 用户
$user_id = (int)$orderInfo['user_id'];

// 判断订单是否支付
if ($orderInfo['state'] > 1) {
    response(1,'订单已支付',false);
}

//微信支付标题
$wechatPayTitleSql = "SELECT * FROM `weixin_config` WHERE item_key = 'wechat_pay_title'";
$wechatPayTitle = $dbh->query($wechatPayTitleSql)->fetch(2);
if ($wechatPayTitle) {
    $title = $wechatPayTitle['item_value'];
} else {
    $title = 'VIP认证费用';
}

//微信支付接口
$orderArgsObj = new WxPayUnifiedOrder();

//设置必填参数
$orderArgsObj->SetOut_trade_no( $orderInfo['code']);
$orderArgsObj->SetBody($title);
$orderArgsObj->SetTotal_fee($orderInfo['amount'] * 100);//单位是分
$orderArgsObj->SetTrade_type('JSAPI');
$orderArgsObj->SetOpenid($orderInfo['account']);
$orderArgsObj->SetTime_start(date("YmdHis"));
$orderArgsObj->SetTime_expire(date("YmdHis", time() + 30000));
//是否开启微信小程序
//$orderArgsObj->SetWXapp_state(IS_WXAPP);
$wxappState = IS_WXAPP;
//异步通知url未设置，则使用配置文件中的url
$orderArgsObj->SetNotify_url(WxPayConfig::ROB_NOTIFY_URL);//异步通知url
$payResult = WxPayApi::unifiedOrder($orderArgsObj,'',$wxappState);
if ($payResult['result_code'] == 'SUCCESS' && $payResult['return_code'] == 'SUCCESS') {
    $pay = new JsApiPay();
    try{
        $jsApiParameters = $pay->GetJsApiParameters($payResult);
    }catch (Exception $ex){//发生异常重定向
        write_file($logPath, "{$ex->getMessage()}\r\n");
        response(1,$ex->getMessage(),false);
    }
    echo $jsApiParameters;
} else {
    $a = $payResult['err_code'].'_'.$payResult['err_code_des'];
    write_file($logPath, "{$a}\r\n");
    response(1,$a,false);
}